package com.itheima.Service.imple;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.Service.ClazzsService;
import com.itheima.entity.Clazz;
import com.itheima.entity.EmpParam;
import com.itheima.entity.PageBean;
import com.itheima.entity.Student;
import com.itheima.mapper.ClazzsMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@Service
public class ClazzsServiceImpl implements ClazzsService {
    @Autowired
    private ClazzsMapper clazzsMapper;

    @Override
    public PageBean list(EmpParam empParam) {
        //设置启动参数
        PageHelper.startPage(empParam.getPage(), empParam.getPageSize());
        //调用mapper层获取相关数据
        List<Clazz> clazzList = clazzsMapper.list(empParam);
        Page p = (Page) clazzList;
        return new PageBean(p.getTotal(),p.getResult());
    }
    //根据id删除班级
    @Override
    public void deleteById(String id) {
         // 调用mapper层
         //先查询该班级下是否有学生
       List<Student> list= clazzsMapper.selectStudentById(id);
       log.info("list:{}",list);
       if(list.isEmpty()){
           clazzsMapper.deleteById(id);
       }
    }
    //添加班级
    @Override
    public void addClazz(Clazz clazz) {
        //补全数据
        clazz.setCreateTime(LocalDateTime.now());
        clazz.setUpdateTime(LocalDateTime.now());
        //调用mapper层
        clazzsMapper.addClazz(clazz);
    }

    @Override
    public Clazz findById(String id) {
        //调用mapper层
        return  clazzsMapper.findById(id);

    }
    //编辑员工信息
    @Override
    public void updateClazz(Clazz clazz) {
        //调用mapper层
        clazz.setUpdateTime(LocalDateTime.now());
        clazzsMapper.updateClazz(clazz);
    }
    //查询所有班级
    @Override
    public List<Clazz> getAllClazzs() {
        return clazzsMapper.getAllClazzs();
    }
}
